import { FileSystemEntry, FileSystemStats } from '../types';
import { LcDocument } from '@/types/modules/editor/types/editor.type';
import { ITypeEntity } from '@/types/modules/superType/types';
import { getDocumentPath, getTypePath } from './source-path';

/**
 * 检查文档是否完全加载
 */
export function checkDocumentFullyLoaded(doc: LcDocument): boolean {
  return typeof doc.schema !== 'undefined';
}

/**
 * 检查类型实体是否完全加载
 */
export function checkTypeFullyLoaded(type: ITypeEntity): boolean {
  let isFullyLoaded = false;
  switch (type.type) {
    case 'action':
      isFullyLoaded = !!type.source || !!type.code;
      break;
    case 'data':
      isFullyLoaded = !!type.fields;
      break;
    case 'vo':
      isFullyLoaded = !!type.fields;
      break;
    case 'api':
      isFullyLoaded =
        !!type.fields || !!type.params || !!type.query || !!type.pathParams;
      break;
    case 'code':
      isFullyLoaded = !!type.source;
      break;
    case 'cloud_function':
      isFullyLoaded = !!type.code || !!type.source;
      break;
    default:
      isFullyLoaded = false;
  }
  return isFullyLoaded;
}

/**
 * 为文档创建 FileSystemEntry
 */
export function createDocumentEntry(doc: LcDocument): FileSystemEntry {
  const path = getDocumentPath(doc);

  return {
    path,
    content: JSON.stringify(doc, undefined, 2),
    stats: {
      type: 'file',
      size: 0,
      createTime: doc?.create_time || Date.now(),
      modifyTime: Date.now(),
      isMetadataOnly: false,
      isFullyLoaded: checkDocumentFullyLoaded(doc),
      isDocument: true,
      componentType: doc.type,
      // 添加哈希字段，优先使用 doc.hash，否则使用 update_time 作为后备
      contentHash: doc.hash || String(doc.update_time || Date.now()),
      hashTimestamp: Date.now(),
      hashAlgorithm: doc.hash ? 'doc_hash' : 'update_time',
    },
  };
}

/**
 * 为类型实体创建 FileSystemEntry
 */
export function createTypeEntry(
  type: ITypeEntity,
  category?: string,
): FileSystemEntry {
  const path = getTypePath(type, category);

  return {
    path,
    content: JSON.stringify(type, undefined, 2),
    stats: {
      type: 'file',
      size: 0,
      createTime: type.create_time || Date.now(),
      modifyTime: type.update_time || Date.now(),
      isFullyLoaded: checkTypeFullyLoaded(type),
      isMetadataOnly: true,
      // 添加哈希字段，优先使用 type.hash，否则使用 update_time 作为后备
      contentHash: type.hash || String(type.update_time || Date.now()),
      hashTimestamp: Date.now(),
      hashAlgorithm: type.hash ? 'type_hash' : 'update_time',
    },
  };
}

/**
 * 为代码文件创建 FileSystemEntry
 */
export function createCodeEntry(
  path: string,
  content: string,
  options: {
    isDocument?: boolean;
    componentType?: string;
    isFullyLoaded?: boolean;
    createTime?: number;
    modifyTime?: number;
  } = {},
): FileSystemEntry {
  const timestamp = Date.now();

  return {
    path,
    content,
    stats: {
      type: 'file',
      size: content.length,
      createTime: options.createTime || timestamp,
      modifyTime: options.modifyTime || timestamp,
      isDocument: options.isDocument || false,
      componentType: options.componentType,
      isFullyLoaded: options.isFullyLoaded !== false, // 默认为 true
    },
  };
}

/**
 * 为目录创建 FileSystemEntry
 */
export function createDirectoryEntry(
  path: string,
  folderType?: ITypeEntity,
): FileSystemEntry {
  const timestamp = Date.now();

  return {
    path,
    content: folderType ? JSON.stringify(folderType, null, 2) : '',
    stats: {
      type: 'directory',
      size: 0,
      createTime: folderType?.create_time || timestamp,
      modifyTime: folderType?.update_time || timestamp,
    },
  };
}

/**
 * 为新建类型文件创建基础类型实体
 */
export function createBaseTypeFromPath(
  projectId: string,
  path: string,
  content: string,
  fileInfo: {
    type: string;
    name: string;
    desc: string;
    folder: string;
    extName: string;
  },
): ITypeEntity {
  const timestamp = Date.now();

  const baseType: ITypeEntity = {
    name: fileInfo.name,
    desc: fileInfo.desc,
    folder: fileInfo.folder,
    folderPath: fileInfo.folder,
    projectId,
    create_time: timestamp,
    update_time: timestamp,
  } as ITypeEntity;

  // 根据扩展名确定类型
  switch (fileInfo.extName) {
    case 'm.ts':
      return {
        ...baseType,
        type: 'vo',
        source: content,
      } as ITypeEntity;
    case 'api.ts':
      return {
        ...baseType,
        type: 'api',
        source: content,
      } as ITypeEntity;
    case 'node.ts':
      return {
        ...baseType,
        type: 'cloud_function',
        source: content,
      } as ITypeEntity;
    case 'action.ts':
    case 'util.ts':
      return {
        ...baseType,
        type: 'action',
        source: content,
      } as ITypeEntity;
    case 'flow':
      return {
        ...baseType,
        type: 'flow',
        source: content,
      } as ITypeEntity;
    case 'draw':
      return {
        ...baseType,
        type: 'draw',
        source: content,
      } as ITypeEntity;
    case 'diagram':
      return {
        ...baseType,
        type: 'diagram',
        source: content,
      } as unknown as ITypeEntity;
    case 'ts':
      return {
        ...baseType,
        type: 'typescript',
        source: content,
        language: 'typescript',
      } as ITypeEntity;
    case 'js':
      return {
        ...baseType,
        type: 'javascript',
        source: content,
        language: 'javascript',
      } as ITypeEntity;
    case 'txt':
      return {
        ...baseType,
        type: 'text',
        source: content,
        language: 'text',
      } as ITypeEntity;
    case 'sql':
      return {
        ...baseType,
        type: 'sql',
        source: content,
        language: 'sql',
      } as ITypeEntity;
    case 'json':
      return {
        ...baseType,
        type: 'json',
        source: content,
        language: 'json',
      } as ITypeEntity;
    default:
      return {
        ...baseType,
        type: 'code',
        source: content,
      } as ITypeEntity;
  }
}
